package com.yangjun.service.bussiness;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yangjun.dao.mybatis.mapper.businessAnalysis.logisticsAnalysis.PackageEfficacyMapper;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.CollectGoodsFailEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.HostSaleGroupDetailsEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.HostSaleSigleEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.OverviewFormEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.OverviewFormExcelEntity;
import com.yangjun.entity.businessAnalysis.logisticsAnalysis.RefundPackageDetailEntity;
import com.yangjun.utils.export.ExcelExportService;
import com.yangjun.utils.export.ExportExcel;

import jxl.Workbook;
import jxl.write.WritableWorkbook;


/**
 * 包裹时效--导出excel
 * @author lijianhua
 *
 */
@Service
public class PackageEfficacyExportService extends ExcelExportService{
	
	@Autowired
	private PackageEfficacyMapper mapper;
	

	/**
	 * 返回流给web端
	 */
	public void putDataOnOutputStream(int  tableFlag, OutputStream os,Map<String,String> map) {
		WritableWorkbook workbook;
		try {
			workbook = Workbook.createWorkbook(os);
			if (tableFlag==1) {
				List<HostSaleSigleEntity> hostSaleSigle = mapper.hostSaleSigle(map.get("platform"), map.get("urgenttype"), map.get("cycle"));
//				for (HostSaleSigleEntity hs : hostSaleSigle) {
//					System.out.println(hs);
//				}
				String[] titles={"排名","商品编码","商品名称","预定次数","库存","销量","销额","近7日销量","近7日销额"};
				String[] fields={"order_rank","warecode","warename","orderqty","stockqty","saleqty","saleamt","saleqty_last7","saleamt_last7"};
				ExportExcel<HostSaleSigleEntity> export = new ExportExcel<HostSaleSigleEntity>();
				export.fillExcel(workbook, HostSaleSigleEntity.class,
						titles, fields,
						"热销单品数据", hostSaleSigle);
			}else if(tableFlag==2) {
//				System.err.println("xxx热销组合详情数据xxxx");
				
				List<HostSaleGroupDetailsEntity> hostSaleGroupDetails = mapper.hostSaleGroupDetailsExcel(map.get("platform"), map.get("urgenttype"), map.get("cycle"));
				String[] titles={"组合","商品编码","商品名称","规格","销量","销额","近7日销量","近7日销额"};
				String[] fields={"kitcode","warecode","warename","model","saleqty","saleamt","saleqty_last7","saleamt_last7"};
				ExportExcel<HostSaleGroupDetailsEntity> export = new ExportExcel<HostSaleGroupDetailsEntity>();
				export.fillExcel(workbook, HostSaleGroupDetailsEntity.class,
						titles, fields,
						"热销组合详情数据", hostSaleGroupDetails);
			}else if(tableFlag==3) {
				List<CollectGoodsFailEntity> collectGoodsFail = mapper.collectGoodsFail();
				String[] titles={"商品编码","商品名称","规格","包裹数量","行目数"," 商品数量"};
				String[] fields={"warecode","warename","model","cons_qty","sku_qty","ware_qty"};
				ExportExcel<CollectGoodsFailEntity> export = new ExportExcel<CollectGoodsFailEntity>();
				export.fillExcel(workbook, CollectGoodsFailEntity.class,
						titles, fields,
						"集货失败数据", collectGoodsFail);
			}else if(tableFlag==4) {
//				System.err.println("退货包裹明细下载");
				List<RefundPackageDetailEntity> refundPackage = mapper.refundPackage(map.get("platform"));
				String[] titles={"日期","平台","快递公司","包裹数","行目数","拒收包裹","正品包裹","次品包裹"};
				String[] fields={"date_day","ordersourcename","rtntransfername","consqty","warerows","cons_reject_qty","goodqty","badqty"};
				ExportExcel<RefundPackageDetailEntity> export = new ExportExcel<RefundPackageDetailEntity>();
				export.fillExcel(workbook, RefundPackageDetailEntity.class,
						titles, fields,
						"退货包裹明细数据", refundPackage);
			}else if(tableFlag==5){
				List<OverviewFormExcelEntity> overviewForm = mapper.overviewFormExcel();
				String[] titles={"日期","包裹类型","同步包裹数","创建拣货单包裹","拣货完成包裹","复核包裹数","称重包裹数","揽收包裹数",
						"同步-创建拣货单","创建拣货单-开始拣货","开始拣货-完成拣货","完成拣货-复核","复核-称重","称重-揽收"};
				String[] fields={"data_date","urgenttype","sys_cons_qty","build_cons_qty","pke_cons_qty","reck_cons_qty","weight_cons_qty","collect_cons_qty",
						"sys_build","build_pks","pks_pke","pke_reck","reck_weight","weight_collect"};
				ExportExcel<OverviewFormExcelEntity> export = new ExportExcel<OverviewFormExcelEntity>();
				export.fillExcel(workbook, OverviewFormExcelEntity.class,
						titles, fields,
						"近30天物流效能数据", overviewForm);
			}
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	/**
	 * 返回不选平台 不选包裹类型时 总况 表格数据
	 * @return
	 */
	public OverviewFormEntity getOverviewForm(String platform ,String urgenttype){
		OverviewFormEntity overviewForm = mapper.overviewForm(platform,urgenttype);
    	return overviewForm;
		
	}
}
